package pl.iachoo.ctl.tabory.ui.editors.stations;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import net.sourceforge.nattable.NatTable;
import net.sourceforge.nattable.data.IDataProvider;
import net.sourceforge.nattable.grid.data.DefaultColumnHeaderDataProvider;
import net.sourceforge.nattable.grid.data.DefaultCornerDataProvider;
import net.sourceforge.nattable.grid.data.DefaultRowHeaderDataProvider;
import net.sourceforge.nattable.grid.layer.CornerLayer;
import net.sourceforge.nattable.grid.layer.GridLayer;
import net.sourceforge.nattable.layer.DataLayer;

import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.jface.layout.GridLayoutFactory;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.forms.SectionPart;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.Section;

import pl.iachoo.ctl.tabory.dao.data.vo.StationVO;
import pl.iachoo.ctl.tabory.ui.editors.stations.nattable.BodyLayerStack;
import pl.iachoo.ctl.tabory.ui.editors.stations.nattable.ColumnHeaderLayerStack;
import pl.iachoo.ctl.tabory.ui.editors.stations.nattable.RowHeaderLayerStack;
import pl.iachoo.ctl.tabory.ui.editors.stations.nattable.StationDataProvider;
import pl.iachoo.ctl.tabory.ui.selection.ISelectionListener;
import pl.iachoo.ctl.tabory.ui.selection.ISelectionProvider;

public class StationsDistancesNatNatbleSection extends SectionPart implements
		ISelectionProvider<StationSelection> {

	private StationSelection selection;

	public StationsDistancesNatNatbleSection(Composite parent,
			FormToolkit toolkit, int style) {
		super(parent, toolkit, style);
		Section parentSection = getSection();
		parentSection.setText("'edleglosci:'");
		GridLayoutFactory.fillDefaults().margins(3, 3).numColumns(1)
				.applyTo(parentSection);

		Composite container = toolkit.createComposite(parentSection);
		GridLayoutFactory.fillDefaults().numColumns(1).applyTo(container);
		GridDataFactory.fillDefaults().align(SWT.FILL, SWT.FILL)
				.grab(true, true).applyTo(container);
		parentSection.setClient(container);

		List<StationVO> listStation = new ArrayList<StationVO>();
		for (int i = 0; i < 10; i++) {
			StationVO station = new StationVO();
			station.setActive(true);
			station.setDateCreated(new Date());
			station.setId(i);
			station.setStationName("station " + i);
			listStation.add(station);
		}

		IDataProvider bodyDataProvider = new StationDataProvider(listStation);
		String[] columnLabels = new String[listStation.size()];
		for (StationVO station : listStation) {
			columnLabels[listStation.indexOf(station)] = station
					.getStationName();
		}

		DefaultColumnHeaderDataProvider colHeaderDataProvider = new DefaultColumnHeaderDataProvider(
				columnLabels);
		DefaultRowHeaderDataProvider rowHeaderDataProvider = new DefaultRowHeaderDataProvider(
				bodyDataProvider);

		BodyLayerStack bodyLayer = new BodyLayerStack(bodyDataProvider);
		ColumnHeaderLayerStack columnHeaderLayer = new ColumnHeaderLayerStack(
				colHeaderDataProvider, bodyLayer);
		RowHeaderLayerStack rowHeaderLayer = new RowHeaderLayerStack(
				rowHeaderDataProvider, bodyLayer);
		DefaultCornerDataProvider cornerDataProvider = new DefaultCornerDataProvider(
				colHeaderDataProvider, rowHeaderDataProvider);
		CornerLayer cornerLayer = new CornerLayer(new DataLayer(
				cornerDataProvider), rowHeaderLayer, columnHeaderLayer);

		GridLayer gridLayer = new GridLayer(bodyLayer, columnHeaderLayer,
				rowHeaderLayer, cornerLayer);
		NatTable natTable = new NatTable(container, gridLayer);
		GridDataFactory.fillDefaults().align(SWT.FILL, SWT.FILL)
				.grab(true, true).applyTo(natTable);
		toolkit.paintBordersFor(natTable);
		toolkit.adapt(natTable);
	}

	@Override
	public StationSelection getSelection() {
		return selection;
	}

	@Override
	public void setSelection(StationSelection selection) {
		this.selection = selection;
	}

	@Override
	public void addSelectionListener(
			ISelectionListener<StationSelection> listner) {
		// TODO Auto-generated method stub

	}

	@Override
	public void removeSelectionListener(
			ISelectionListener<StationSelection> listner) {
		// TODO Auto-generated method stub

	}

}
